﻿///////////////////////////////////////////////////////////////////////////////
// File:        SocketThread.hpp
// Author:      671643387@qq.com
// Date:        2016年1月5日 下午5:43:46
// Description:
///////////////////////////////////////////////////////////////////////////////

#ifndef NET_SOCKETTHREAD_HPP_
#define NET_SOCKETTHREAD_HPP_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <boost/shared_ptr.hpp>
#include <boost/atomic.hpp>
#include <fog/Typedef.hpp>
#include <fog/thread/Thread.hpp>
#include <fog/net/Socket.hpp>

namespace fog
{
    namespace net
    {
        class SocketManager;

        class SocketThread : public thread::Thread
        {
        public:
            SocketThread(void);
            ~SocketThread(void);

        public:
            bool open(bool notCreateThread = false);
            virtual void stop(void);

            void incLoad(void) { ++load_; }
            void decLoad(void) { --load_; }
            std::uint32_t getLoad(void) const { return load_; }
        protected:
            virtual void run(void);

        private:
            void doClose(void);
            static void onDummy(evutil_socket_t sock, short event, void* arg);

        private:
            friend class SocketManager;
            friend class Socket;

        private:
            struct event_base *evBase_;
            struct event *evDummy_;
            boost::atomic_uint32_t load_;
        };

        typedef boost::shared_ptr<SocketThread>   SocketThreadPtr;
    }
}

#endif /* NET_SOCKETTHREAD_HPP_ */
